<?php
class Admin_AdsController extends App_ControllerAdmin
{
    public function init()
    {
		$this->_helper->layout->setLayout('admin');
		$this->_sess = new Zend_Session_Namespace();
   		if(!@$this->_sess->session){
			$this->_helper->redirector('index','login','admin');
		}
	}
	
	public function indexAction(){
		$ads = new Admin_Model_Ads;
		$ads = $ads -> view();
		$this->view->view_ads = $ads;
	}
	
	public function addAction(){
		$location = new Admin_Model_Location;
		$ads = new Admin_Model_Ads;
		if($this->_request->isPost()){
			$Request = $this->getRequest()->getPost();
			$date = date("Y-m-d");
			$upload = new Zend_File_Transfer_Adapter_Http();
         	$info = $upload->getFileInfo();
         	$picture = $info['images'];
         	$upload->setDestination('public/images/ads/');
         	$upload->receive($picture['name']);
			$image['images']=$picture['name'];
			$data = array(
						'id_user'				=> $this->_sess->session['id'],
						'customers'				=> $Request['customers'],
						'description'  			=> $Request['description'],
						'url'					=> $Request['url'],
						'time_lock'				=> $Request['time'],
						'images'				=> $image['images'],
						'create_date' 			=> $date,
						'location'				=> $Request['location'],
						'is_delete'				=> $Request['delete'],
						
			);
			$ok = $ads->add($data);
			$this->view->message = 'Thêm Dữ Liệu Thành Công !!!';	
		}
		
		$location = $location -> view();
		$this->view->location = $location;
	}
	
	public function editAction(){
		$location = new Admin_Model_Location;
		$ads = new Admin_Model_Ads;
		$id = $this->getRequest()->getParam('id');
		$data = $ads->view_where($id);
		$this->view->view_ads_where = $data;
		if($this->_request->isPost()){
			$Request = $this->getRequest()->getPost();
			$upload = new Zend_File_Transfer_Adapter_Http();
         	$info = $upload->getFileInfo();
         	$picture = $info['images'];
         	$upload->setDestination('public/images/ads/');
         	$upload->receive($picture['name']);
			$image['images']=$picture['name'];
			if($image['images']==NULL)
			{
				$data = array(
						'customers'				=> $Request['customers'],
						'description'  			=> $Request['description'],
						'url'					=> $Request['url'],
						'time_lock'				=> $Request['time'],
						'location'				=> $Request['location'],
						'is_delete'				=> $Request['delete'],
						
				);
			}
			else{
				$data = array(
						'customers'				=> $Request['customers'],
						'description'  			=> $Request['description'],
						'url'					=> $Request['url'],
						'time_lock'				=> $Request['time'],
						'images'				=> $image['images'],
						'location'				=> $Request['location'],
						'is_delete'				=> $Request['delete'],
						
				);
			}
			$ok = $ads->edit($id,$data);
			$this->view->message = 'Thêm Dữ Liệu Thành Công !!!';	
		}
		
		$location = $location -> view();
		$this->view->location = $location;
		
	}
	
	public function lockAction(){
		$mode = new Admin_Model_Ads;
		$id = $this->getRequest()->getParam('id');
		if($id){
			$data = array(
					'is_delete'	=> 1,
			);
			$mode->lock($id,$data);
			$this->view->message = 'Xóa Dữ Liệu Thành Công !!!';
		}
		$this->_helper->redirector('');
	}
	
}
